/*!
 * Ext JS Library 3.1.1
 * Copyright(c) 2006-2010 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
QzHouseGrid = function(viewer, config) {
    this.viewer = viewer;
    Ext.apply(this, config);

    this.store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/desktop/get_qzxx'
        }),
        reader: new Ext.data.JsonReader({
          totalProperty: 'results', 
          root: 'rows',             
          fields: [
          {name: 'id'},
          {name: 'kybh',  type: 'string'},
          {name: 'kh_id', type: 'integer'},
          {name: 'khxm',  type: 'string'},
          {name: 'khlxr', type: 'string'},
          {name: 'khsfz', type: 'string'},
          {name: 'khdz',  type: 'string'},
          {name: 'khdh',  type: 'string'},
          {name: 'khdj',  type: 'string'},
          {name: 'khxz',  type: 'string'},
          {name: 'khlb',  type: 'string'},
          {name: 'qzyx',  type: 'string'},
          {name: 'khgj',  type: 'string'},
          {name: 'khqx',  type: 'string'},
          {name: 'jy',  type: 'string'},
          {name: 'zt',  type: 'string'},
          {name: 'gs',  type: 'string'},
          {name: 'yzk', type: 'string'},
          {name: 'cq',  type: 'string'},
          {name: 'pq',  type: 'string'},
          {name: 'mpdh',  type: 'string'},
          {name: 'ygmj',  type: 'integer'},
          {name: 'ssmj',  type: 'integer'},
          {name: 'bgmj',  type: 'integer'},
          {name: 'kdmj',  type: 'integer'},
          {name: 'cf_min',  type: 'integer'},
          {name: 'cf_max',  type: 'integer'},
          {name: 'jg_min',  type: 'real'},
          {name: 'jg_max',  type: 'real'},
          {name: 'create_at', type: 'date', dateFormat: 'Y-m-d H:i:s'},
          {name: 'wtfs',  type: 'string'},
          {name: 'wtly',  type: 'string'},
          {name: 'yt',  type: 'string'},
          {name: 'dgl', type: 'string'},
          {name: 'lx',  type: 'string'},
          {name: 'fwwq',  type: 'string'},
          {name: 'fwpt',  type: 'string'},
          {name: 'cs',  type: 'string'},
          {name: 'czfs',  type: 'string'},
          {name: 'fyfs',  type: 'string'},
          {name: 'bz',  type: 'string'},
          {name: 'yg_id', type: 'integer'},
          {name: 'ygbm',  type: 'string'},
          {name: 'ygxm',  type: 'string'},
          {name: 'txzt',  type: 'string'},
          {name: 'lxfs',  type: 'string'},
          {name: 'update_at', type: 'date', dateFormat: 'Y-m-d H:i:s'},   
          {name: 'process', type: 'string'}   
          ]    
        }),
        sortInfo:{field: 'update_at', direction: "DESC"},
        baseParams: {ygxm:currentUser.login, start:0, limit:15, query:""}
    });
    
    this.store.reload();
    
    this.store.setDefaultSort('update_at', "DESC");


    this.columns = [
      { id: 'id', header: "id", dataIndex: 'id', hidden: true, width: 25,sortable: true},
      { header: "!",      dataIndex: 'process',  width: 20, sortable:true},
      { header: "客源编号",   dataIndex: 'kybh',  width: 150, sortable:true},
      { header: "客源姓名",   dataIndex: 'khxm',  width: 150, sortable:true},
      { header: "交易",     dataIndex: 'jy',    width: 75, sortable: true},
      { header: "委托日",    dataIndex: 'create_at',   width: 200, sortable:true, renderer: Ext.util.Format.dateRenderer('Y-m-d')},
      { header: "城区",     dataIndex: 'cq',    width: 100, sortable: true},
      { header: "片区",     dataIndex: 'pq',  hidden: true, width: 100, sortable: true},
      { header: "位置",     dataIndex: 'mpdh',  hidden:true,  width: 75, sortable:true},
      { header: "电功率",    dataIndex: 'dgl',   width: 100, sortable:true},
      { header: "面积~",    dataIndex: 'cf_min',    width: 100, sortable:true},
      { header: "~面积",    dataIndex: 'cf_max',    width: 100, sortable:true},
      { header: "层数",     dataIndex: 'cs',    width: 100, sortable:true},
      { header: "价格",     dataIndex: 'jg_max',    width: 100, sortable:true},
      { header: "围墙",     dataIndex: 'fwwq',  hidden: true, width: 100, sortable:true},
      { header: "配套",     dataIndex: 'fwpt',  hidden: true, width: 100, sortable:true},
      { header: "用途",     dataIndex: 'yt',    width: 150, sortable:true},
      { header: "等级",     dataIndex: 'khdj',    width: 100, sortable:true},
      { header: "员工",     dataIndex: 'ygxm',    width: 150, sortable:true},
      { header: "状态",     dataIndex: 'zt',    width: 150, sortable:true},
      { header: "最后跟进日",  dataIndex: 'update_at',   width: 200, sortable:true, renderer: Ext.util.Format.dateRenderer('Y-m-d')},
      { header: "委托",     dataIndex: 'wtfs',    hidden: true, width: 100, sortable:true},
      { header: "来源",     dataIndex: 'wtly',    hidden: true, width: 100, sortable:true},
      { header: "类别",     dataIndex: 'khlb',    hidden: true, width: 100, sortable:true},
      { header: "意向",     dataIndex: 'qzyx',    hidden: true, width: 100, sortable:true},
      { header: "期限",     dataIndex: 'khqx',    hidden: true, width: 100, sortable:true}
    ];

    QzHouseGrid.superclass.constructor.call(this, {
        region: 'center',
        id: 'qz-house-grid',
        loadMask: {msg:'正在载入...'},

        sm: new Ext.grid.RowSelectionModel({
            singleSelect:true
        }),
       
        //sm : new Ext.grid.CheckboxSelectionModel();

        stripeRows:true,

        viewConfig: {
            forceFit:true,
            enableRowBody:true,
            showPreview:true,
            getRowClass : this.applyRowClass
        }
    });

    this.on('rowcontextmenu', this.onContextClick, this);
};

var qz_click_handler = function(item, e) {
    var pars = {id:qzxx_id, name:item.text};
    new Ajax.Request("/desktop/trans_qzxx", { 
      method: "POST",
        parameters: pars,
        onComplete:  function(request) {
          Ext.getCmp('qzxx-view').reloadStore();
        }
    });   
}

var qz_otherMenu = new Ext.menu.Menu({
    id: 'qz_other_person',
    items:[{text:'王娅红', iconCls: 'user', handler:qz_click_handler},{text:'胡启龙', iconCls: 'user', handler:qz_click_handler},{text:'汪玲玲', iconCls: 'user', handler:qz_click_handler},{text:'夏小丽', iconCls: 'user', handler:qz_click_handler},{text:'公共帐户', iconCls: 'user', handler:qz_click_handler},{text:'李焱', iconCls: 'user', handler:qz_click_handler},{text:'程慧杰', iconCls: 'user', handler:qz_click_handler},{text:'朱媛媛', iconCls: 'user', handler:qz_click_handler},{text:'许萍', iconCls: 'user', handler:qz_click_handler},{text:'陶淑君', iconCls: 'user', handler:qz_click_handler},{text:'王洪利', iconCls: 'user', handler:qz_click_handler},{text:'陆永迪', iconCls: 'user', handler:qz_click_handler},{text:'许青', iconCls: 'user', handler:qz_click_handler},{text:'刘华影', iconCls: 'user', handler:qz_click_handler},{text:'刘主管', iconCls: 'user', handler:qz_click_handler},{text:'朱主管', iconCls: 'user', handler:qz_click_handler}]
});


Ext.extend(QzHouseGrid, Ext.grid.EditorGridPanel, {

    onContextClick : function(grid, index, e){
        
        if(!this.menu){ // create context menu on first right click
           // new Ajax.Request("/desktop/get_qzuser_menu", {
           //     method: "POST",
           //     onComplete:  function(request) {
           //     var qz_menu = Ext.getCmp('qz_other_person');
           //       ss=eval(request.responseText);
           //       for (var i=0; i < ss.size(); i ++) {
           //           qz_menu.addItem(ss[i]);
           //       }
           //   }
           // });
                  
            this.menu = new Ext.menu.Menu({
                id:'qz-task-ctx',
                items: [{
                    iconCls: 'new-win',
                    text: '匹配房源',
                    scope:this,
                    handler: function(){
                      //window.open(this.ctxRecord.data.link);
                      //MyDesktop.GridWindow.createWindow();
                      MyDesktop.modules[1].createWindow();

                      var data = this.ctxRecord.data
                      
                      var btn_7 = Ext.getCmp('czxx-view').getTopToolbar().items.get(7);
                      btn_7.setValue(data.cq);
                      
                      var form = Ext.getCmp('czxx_north_search').items.items[0].getForm();
                      form.findField('mj1').setValue(data.cf_min);
                      form.findField('mj2').setValue(data.cf_max);
                      
                      //区县，面积两个条件进行与选择
                      var paras = {search:data.cq, mj1:data.cf_min, mj2:data.cf_max, start:0, limit:25};
                      Ext.getCmp('czxx-view').grid.loadData(paras);
                    }
                },
                {
                    iconCls: 'new-win',
                    text: '转他人',
                    menu: qz_otherMenu
                },
                '-',
                {
                    iconCls: 'refresh',
                    text:'刷新',
                    scope:this,
                    handler: function(){
                      this.ctxRow = null;
                      this.store.reload();
                    }
                },{
                  iconCls: 'delete',
                  text:'删除记录',
                  scope:this,
                  handler: function(){
                      //var pars = "id=" + this.ctxRecord.id;
                      var pars = {id:this.ctxRecord.id};
                      var ctxRow = this.ctxRow;
                      var cur_store = this.store;
                      Ext.Msg.confirm('信息','确定要删除？', function(btn){
                        if (btn == 'yes') {
                          //var pars = {id:this.ctxRecord.id};
                          new Ajax.Request("/desktop/delete_qzxx", { 
                            method: "POST",
                              parameters: pars,
                              onComplete:  function(request) {
                                //this.ctxRow = null;
                                cur_store.reload();
                              }
                          });                         
                        }
                      });
                  }   
                }]
            });
            // add menu item
            this.menu.on('hide', this.onContextHide, this);
        }
        
        e.stopEvent();
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
        this.ctxRow = this.view.getRow(index);
        this.ctxRecord = this.store.getAt(index);
        Ext.fly(this.ctxRow).addClass('x-node-ctx');

        if (currentUser.prev_code[6] != 't') {
          Ext.getCmp('qz-task-ctx').items['items'][4].hide();
        } else {
          Ext.getCmp('qz-task-ctx').items['items'][4].show();
        }
        
        if　( currentUser.id != parseInt (this.ctxRecord.data.yg_id) ) {
          Ext.getCmp('qz-task-ctx').items['items'][4].hide();
        } else {
          Ext.getCmp('qz-task-ctx').items['items'][4].show();
        }

        if (currentUser.prev_code[9] != 't') {
          Ext.getCmp('qz-task-ctx').items['items'][1].hide();
        } else {
          Ext.getCmp('qz-task-ctx').items['items'][1].show();
        }
        
        this.menu.showAt(e.getXY());
    },

    onContextHide : function(){
        if(this.ctxRow){
            Ext.fly(this.ctxRow).removeClass('x-node-ctx');
            this.ctxRow = null;
        }
    },

    loadData : function(paras) {
        this.store.baseParams = paras;
        this.store.load();
    },

    formatDate : function(date) {
        if (!date) {
            return '';
        }
        var now = new Date();
        var d = now.clearTime(true);
        var notime = date.clearTime(true).getTime();
        if (notime == d.getTime()) {
            return 'Today ' + date.dateFormat('g:i a');
        }
        d = d.add('d', -6);
        if (d.getTime() <= notime) {
            return date.dateFormat('D g:i a');
        }
        return date.dateFormat('n/j g:i a');
    },

    formatTitle: function(value, p, record) {
        return String.format(
                '<div class="topic"><b>{0}</b><span class="author">{1}</span></div>',
                value, record.data.author, record.id, record.data.forumid
                );
    },

    applyRowClass: function(record, rowIndex, p, ds) {
      var cls = 'white-row';
      switch (record.data.txzt) {
        case '提醒' :
          cls = 'yellow-row';
          break;
        case '公共' :
          cls = 'red-row';
          break;
      }
      return cls;
    },

    formatkey: function(value) {
      if (value == '有匙') {
        return "<img src='/images/k_2.png' />";
      } else {
        return "";
      }
    }
});

Ext.reg('qzhousegrid', QzHouseGrid);